package com.zhkj.txg.module.home.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.MallCategoryShopEntity
import com.zhkj.txg.module.home.entity.MallShopEntity
import com.zhkj.txg.module.home.ui.ShopActivity
import com.zhkj.txg.utils.ImageUtils

class MallCategoryShopAdapter :
    BaseNodeAdapter(), LoadMoreModule {
    companion object {
        val TYPE_SHOP = 0
        val TYPE_ICON = 1
    }

    init {
        addFullSpanNodeProvider(ShopProductProvider())
        addNodeProvider(ShopProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is MallShopEntity -> TYPE_SHOP
            is MallCategoryShopEntity -> TYPE_ICON
            else -> TYPE_ICON
        }
    }
}

class ShopProductProvider : BaseNodeProvider() {
    override val itemViewType: Int = 0
    override val layoutId: Int = R.layout.item_mall

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        (viewHolder.itemView.layoutParams as? ViewGroup.MarginLayoutParams)?.marginStart = 0
        with(viewHolder.getView<RecyclerView>(R.id.rvMall)) {
            hasFixedSize()
            layoutManager = LinearLayoutManager(
                context, LinearLayoutManager.HORIZONTAL, false
            )
            adapter = MallProductAdapter()
        }
    }

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? MallShopEntity)?.let {
            val imageView = helper.setText(R.id.tvMallShopName, it.shopName)
                .getView<ImageView>(R.id.ivMallShop)
            ImageUtils.loadCircleImage(context, imageView, it.shopImg)
            (helper.getView<RecyclerView>(R.id.rvMall).adapter as MallProductAdapter).setNewData(it.goods)
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? MallShopEntity)?.let {
            if (data.id > 0) ShopActivity.open(context, data.shopName, data.id)
        }
    }
}

class ShopProvider : BaseNodeProvider() {
    override val itemViewType: Int = 1
    override val layoutId: Int = R.layout.item_home_cate

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? MallCategoryShopEntity)?.let {
            val imageView = helper.setText(R.id.tvCate, it.shopName).getView<ImageView>(R.id.ivCate)
            ImageUtils.loadCircleImage(context, imageView, it.shopImg)
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? MallCategoryShopEntity)?.let {
            ShopActivity.open(context, it.shopName, it.id)
        }
    }
}